package com.xiaodeng.xdblogspringboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaodeng.xdblogspringboot.exception.ApplicationException;
import com.xiaodeng.xdblogspringboot.mapper.ApplicationMapper;
import com.xiaodeng.xdblogspringboot.mapper.ArticleMapper;
import com.xiaodeng.xdblogspringboot.mapper.UserMapper;
import com.xiaodeng.xdblogspringboot.pojo.Application;
import com.xiaodeng.xdblogspringboot.pojo.DTO.ApplicationDTO;
import com.xiaodeng.xdblogspringboot.pojo.PageBean;
import com.xiaodeng.xdblogspringboot.service.ApplicationService;
import com.xiaodeng.xdblogspringboot.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ApplicationServiceImpl implements ApplicationService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void auditApplication(Application application) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer auditorId = (Integer) map.get("id");
        application.setAuditorId(auditorId);
        application.setAuditTime(LocalDateTime.now());
        // 根据审核状态进行处理
        if ("审核通过".equals(application.getAuditStatus())) {
            // 审核通过，不需要理由
            application.setAuditReason(null);  // 清空拒绝理由
            application.setAuditStatus("审核通过");
            applicationMapper.auditApplication(application);
        } else if ("审核失败".equals(application.getAuditStatus())) {
            // 审核失败，需要理由
            if (application.getAuditReason() == null || application.getAuditReason().isEmpty()) {
                // 检查拒绝理由是否为空
                throw new ApplicationException(400,"拒绝理由不能为空");
            }
            // 设置审核结果
            application.setAuditStatus("审核失败");
            // 更新审核结果
            applicationMapper.auditApplication(application);
        } else {
            // 检查审核状态的有效性
            throw new ApplicationException(400,"无效的审核状态");
        }
    }

    @Override
    public PageBean<ApplicationDTO> getAllApplication(Integer pageNum, Integer pageSize, String title, String username, String auditStatus) {
        //创建pageBean对象
        PageBean<ApplicationDTO> pb = new PageBean<>();
        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //调用mapper
        // 根据标题和用户名查询对应的 ID
        List<ApplicationDTO> as = applicationMapper.getAllApplication(title, username, auditStatus);
//        Page<Application> p =(Page<Application>) as;
        // 使用 PageInfo 进行分页处理
        PageInfo<ApplicationDTO> p= new PageInfo<>(as);
        //把数据填充到pageBean
        //获取总条数
        pb.setTotal(p.getTotal());
        //获取当前页的数据
        pb.setItems(p.getList());
        return pb;
    }

    @Override
    public List<ApplicationDTO> getApplication() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return applicationMapper.getApplication(userId);
    }
}
